library("tidyverse")
## ── Attaching packages ─────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.3     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
options(max.print=1000)
surveys_complete <- read.csv("data/surveys_complete.csv")

To build a ggplot, use the function format:

ggplot2(data = , mapping = aes ()) + ()

ggplot(data = surveys_complete)

## define aesthetic mapping (using aes function), by selecting variables to be potted and spcify how to present them (e.g., x/y poistions or characteristics or size, shape, color)

ggplot(data = surveys_complete, mapping = aes(x=weight, y=hindfoot_length))

## add "geoms" - graphical representations of the data in the plot (point, lines, bars). ggplot2 offers many different geoms

ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) + geom_point()


## Assign plot to a variable

```r
surveys_plot <- ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length))

Draw the plot

surveys_plot + geom_point()

Challenge 1: Allows hexagonal binning

install.packages("hexbin")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/4.0'
## (as 'lib' is unspecified)
library("hexbin")

surveys_plot <- ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length))

surveys_plot +
  geom_hex ()

## The strengths of the hexagonal has a key that shows a color code for regions of concentration (e.g., more point, the ligher blue it gets). For weaknesses, outliers are not as apparent and the shape slightly changes, showing a condensed graph. 

To begin building plot, we start by defining the dataset, the axes, and choose a geom:

ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
    geom_point()

Transparency (alpha) can be added to avoid overplotting

ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
    geom_point(alpha = 0.1)

To add colors for all points

ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length)) +
    geom_point(alpha = 0.1, color = "blue")

To add colors to indivdual points, you cause use color and labeling that specific vector

ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length)) +
    geom_point(alpha = 0.1, aes(color = species_id))

Challenge Set 2

## Use what you learned to create a scatter plot of weight over species with the plot types showing in different colors. Is this a good way to show data?

ggplot(data = surveys_complete, 
       mapping = aes(x = species_id, y = weight)) +
   geom_point(aes(color = plot_type))

Boxplots

## Visualizing weight distribution within each species
ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
    geom_boxplot()

Adding points to the boxplot, we can see a better idea of number of measurements and of their distribution

ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
    geom_boxplot(alpha = 0) +
    geom_jitter(alpha = 0.3, color = "tomato")

## Jitter adds the points on top of the box plot, showing the range, means, quartiles, etc.

Challenge Set 3

## violin plot or bean plot can show the shape of the distribution
ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
    geom_violin(alpha = 0) +
    geom_jitter(alpha = 0.3, color = "tomato")

## changing the scale of the axes can show a better distribution of the data

ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
    geom_violin(alpha = 0) +
    geom_jitter(alpha = 0.3, color = "tomato") +
    scale_y_log10()

## making a new plot to explore the distribution of another variable within each species

ggplot(data = surveys_complete, mapping = aes(x = species_id, y = hindfoot_length)) +
geom_jitter(aes(color = plot_id)) +
geom_boxplot(color = "pink")

## changing plot_id from integer to factor

ggplot(data = surveys_complete, mapping = aes(x = species_id, y = hindfoot_length)) +
geom_jitter(aes(color = factor(c(plot_id)))) +
geom_boxplot(color = "black")

Plotting Time Series Data

yearly_counts <- surveys_complete %>%
  count(year, genus)
ggplot(data = yearly_counts, aes(x = year, y = n)) +
     geom_line()

## must define groups to get separate lines
ggplot(data = yearly_counts, aes(x = year, y = n, group = genus)) +
    geom_line()

## can add colors to distinguish groups
ggplot(data = yearly_counts, aes(x = year, y = n, color = genus)) +
    geom_line()

Integrating the pipe operate with ggplot2

yearly_counts %>% 
    ggplot(mapping = aes(x = year, y = n, color = genus)) +
    geom_line()

## can use pipe operator to link data manipulation with consequent data visualization
yearly_counts_graph <- surveys_complete %>%
    count(year, genus) %>% 
    ggplot(mapping = aes(x = year, y = n, color = genus)) +
    geom_line()

yearly_counts_graph

Faceting: allows the split of one plot into mulitple plots based on a factor

ggplot(data = yearly_counts, aes(x = year, y = n)) +
    geom_line() +
    facet_wrap(facets = vars(genus))

 yearly_sex_counts <- surveys_complete %>%
                      count(year, genus, sex)
## adding color
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_wrap(facets =  vars(genus))

## use facet to split categories again
ggplot(data = yearly_sex_counts, 
       mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_grid(rows = vars(sex), cols =  vars(genus))

# one column, facet by rows
ggplot(data = yearly_sex_counts, 
       mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_grid(rows = vars(genus))

# one row, facet by columns
ggplot(data = yearly_sex_counts, 
       mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_grid(cols = vars(genus))

ggplot2 themes

# to get a simple black and white graph
 ggplot(data = yearly_sex_counts, 
        mapping = aes(x = year, y = n, color = sex)) +
     geom_line() +
     facet_wrap(vars(genus)) +
     theme_bw()

Challenge Set 4

yearly_weight <- surveys_complete %>%
                group_by(year, species_id) %>%
                 summarize(avg_weight = mean(weight))
## `summarise()` regrouping output by 'year' (override with `.groups` argument)
ggplot(data = yearly_weight, mapping = aes(x=year, y=avg_weight)) +
   geom_line() +
   facet_wrap(vars(species_id)) +
   theme_bw()

Customization

## changing axes titles
ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex)) +
    geom_line() +
    facet_wrap(vars(genus)) +
    labs(title = "Observed genera through time",
         x = "Year of observation",
         y = "Number of individuals") +
    theme_bw()

## changing axes font size
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, color = sex)) +
    geom_line() +
    facet_wrap(vars(genus)) +
    labs(title = "Observed genera through time",
        x = "Year of observation",
        y = "Number of individuals") +
    theme_bw() +
    theme(text=element_text(size = 16))

## changing axes to fit vertically/horizontally
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, color = sex)) +
    geom_line() +
    facet_wrap(vars(genus)) +
    labs(title = "Observed genera through time",
        x = "Year of observation",
        y = "Number of individuals") +
    theme_bw() +
    theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 90, hjust = 0.5, vjust = 0.5),
                    axis.text.y = element_text(colour = "grey20", size = 12),
                        strip.text = element_text(face = "italic"),
                        text = element_text(size = 16))

## to save the changes to use them later

grey_theme <- theme(axis.text.x = element_text(colour="grey20", size = 12, 
                                               angle = 90, hjust = 0.5, 
                                               vjust = 0.5),
                    axis.text.y = element_text(colour = "grey20", size = 12),
                    text=element_text(size = 16))

ggplot(surveys_complete, aes(x = species_id, y = hindfoot_length)) +
    geom_boxplot() +
    grey_theme

Challenge Set 5

pink_theme <- theme(axis.text.x = element_text(colour="pink", size = 14, 
                                               angle = 90, hjust = 1.0, 
                                               vjust = 1.0),
                    axis.text.y = element_text(colour = "pink", size = 12),
                    text=element_text(size = 16))

ggplot(surveys_complete, aes(x = species_id, y = hindfoot_length)) +
    geom_boxplot(color = "light blue") +
    pink_theme

Arranging and exporting plots

## gridExtra can allow us to produce a single igure that contains multiples plots using different variables or data frames

library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
spp_weight_boxplot <- ggplot(data = surveys_complete, 
                             aes(x = species_id, y = weight)) +
  geom_boxplot() +
  labs(x = "Species", 
       y = expression(log[10](Weight))) +
  scale_y_log10() + 
  labs()

spp_count_plot <- ggplot(data = yearly_counts, 
                         aes(x = year, y = n, color = genus)) +
  geom_line() + 
  labs(x = "Year", y = "Abundance")

grid.arrange(spp_weight_boxplot, spp_count_plot, ncol = 2, widths = c(4, 6))

ggsave function allows you to change dimensions and resolution of plot by adjusting arguments, such as width, height, and dpi

my_plot <- ggplot(data = yearly_sex_counts, 
                  aes(x = year, y = n, color = sex)) +
    geom_line() +
    facet_wrap(vars(genus)) +
    labs(title = "Observed genera through time",
        x = "Year of observation",
        y = "Number of individuals") +
    theme_bw() +
    theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 90,
                                     hjust = 0.5, vjust = 0.5),
          axis.text.y = element_text(colour = "grey20", size = 12),
          text = element_text(size = 16))

ggsave("name_of_file.png", my_plot, width = 15, height = 10)

## This also works for grid.arrange plots

combo_plot <- grid.arrange(spp_weight_boxplot, spp_count_plot, ncol = 2, 
                           widths = c(4, 6))
ggsave("combo_plot_abun_weight.png", combo_plot, width = 10, dpi = 300)
``